const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
require("dotenv").config();

const app = express();

// 中间件配置
app.use(cors());
app.use(morgan("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static("src/public"));

// 路由配置
const userRoutes = require("../routes/users");
const httpStudyRoutes = require("../routes/harmonyOS/httpStudy");
const categoryFoodData = require("../routes/harmonyOS/MEITUAN/CategoryFood");
const fileFragmentRoutes = require("../routes/angularIntegratedProjectVersion7/fileFragmentUpload.js");
const fileBaseUploadRouter = require("../routes/angularIntegratedProjectVersion7/fileBaseUpload.js");
const addAlarmEventRecord = require("../routes/angularIntegratedProjectVersion7/tableFormTabAdd.js");
const inquireAlarmEventRecord = require("../routes/angularIntegratedProjectVersion7/tableFormTabInquire.js");
const operateAlarmEventRecord = require("../routes/angularIntegratedProjectVersion7/tableFormDel.js");
const fileTypeDownload = require("../routes/angularIntegratedProjectVersion7/fileTypeDownload.js");
const componentAssembly = require("../routes/angularIntegratedProjectVersion7/componentAssembly/instructionList.js");

app.get("/", (req, res) => {
    res.json({ message: "欢迎访问 Node API" });
});

app.use("/api/users", userRoutes);
app.use("/api/harmonyOS/httpStudy", httpStudyRoutes);
app.use("/api/harmonyOS/Meituan/CategoryFood", categoryFoodData);
app.use("/api/upload", fileFragmentRoutes);
app.use("/api/upload", fileBaseUploadRouter);
app.use("/api/tableFormBaseTab", addAlarmEventRecord);
app.use("/api/tableFormBaseTab", inquireAlarmEventRecord);
app.use("/api/tableFormBaseTab", operateAlarmEventRecord);

app.use("/api/componentAssembly", componentAssembly);

app.use("/api/fileDownload", fileTypeDownload);

// 错误处理中间件
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).json({ error: "服务器内部错误" });
});

// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
});
